//
//  HomeViewController.swift
//  StudyDY
//
//  Created by yinchangming on 2018/6/29.
//  Copyright © 2018年 yinchangming. All rights reserved.
//

import UIKit

class HomeViewController: BaseViewController {
         let titles=["推荐","游戏","娱乐","趣玩"]
    //懒加载标题控件
    @available(iOS 11.0, *)
    lazy var pageTitleView={ ()->PageTitleView in
        var frame=CGRect(x: 0, y: 64, width: Int(kScreenW), height: kPageTitleH)
        var page = PageTitleView(frame: frame, titles: titles)
        page.delegte=self//代理主要是用来回调被选中页
        return page
    }()
    //懒加载 内容控件
    lazy var pageContentView={ () -> PageContentView in
        //内容页的Frame
        let pageVCFrame=CGRect(x: 0, y: kContentY, width: kScreenW, height: kScreenh - 104)
        //给内容页添加内容
        var vcs=[UIViewController]()
        vcs.append(RecommendViewController())
        vcs.append(GameAllViewController())
        vcs.append(YuLeViewController())
        vcs.append(FunyController())
        let pageVc=PageContentView(frame: pageVCFrame, vcs: vcs, parentVc: self)
        pageVc.backgroundColor=UIColor.brown
        pageVc.delegate=self
        return pageVc
        
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
        upDateUI()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }

}
/**内扩张 主要让类里面代码量少一些 看上去更简洁**/
extension HomeViewController{
    func upDateUI() {
        //1 设置 导航栏
        setNavgation()
        //2 添加顶部菜单栏
        if #available(iOS 11.0, *) {
            view.addSubview(pageTitleView)
        } else {
            // Fallback on earlier versions
        }
        // 3 添加内容控件
        view.addSubview(pageContentView)
    }
    /**设置标题栏**/
    func setNavgation(){
//        self.navigationItem.title="首页"
        self.navigationItem.leftBarButtonItem=UIBarButtonItem.creatBtn(imageNomor: "logo")

        let size=CGSize(width: 40, height: 40)
        //通过对 UIBarButtonItem的构造阔展 来初始化 头部右边的三个按钮
        let historyBtn=UIBarButtonItem(imageNomor: "image_my_history", imageHight: "Image_my_history_click", size: size)
        let searhBtn=UIBarButtonItem(imageNomor: "btn_search", imageHight: "btn_search_clicked", size: size)
        let qrcodeBtn=UIBarButtonItem(imageNomor: "Image_scan", imageHight: "Image_scan_click", size: size)
        self.navigationItem.rightBarButtonItems=[historyBtn,searhBtn,qrcodeBtn]
    }
}

//处理pagetitlelview 选择标题的回调
extension HomeViewController :PageTitleViewProtocol{
    @available(iOS 11.0, *)
    func selecedPage(titleView: PageTitleView, currentPageIndex: Int) {
        pageContentView.setContentPageByTitleView(currentIndex: currentPageIndex)
    }
}
//处理pageContentView 滑动打理

extension HomeViewController : PageContentViewDelegate{
    func pageChageWithProgress(progress: CGFloat, currentPage: Int, mubiaoPage: Int) {
        if #available(iOS 11.0, *) {
            pageTitleView.setCurrentLabelWithProgress(progress: progress, currentPage: currentPage, mubiaoPage: mubiaoPage)
        } else {
            // Fallback on earlier versions
        }
    }
    
    
}
    

